//
//  CusNavView_main.m
//  MGameTribe
//
//  Created by 杨敏 on 13-5-30.
//  Copyright (c) 2013年 YangMin. All rights reserved.
//


#import "CusNavView_main.h"
#import "ConstantHeader.h"
#import "Tool.h"

@implementation CusNavView_main

-(id)initWithFrame:(CGRect)frame{

    frame.size.height=47;
    self=[super initWithFrame:frame];
    if (self) {
        //_leftTitleGap=6;
        
        CGFloat selfHeight =44;
        CGFloat selfWidth =CGRectGetWidth(frame);
        UIImage *navBgImg=ImageWithName(@"bg_topBanner", @"png", NO);
        self.navBgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, selfWidth, 46)];
        _navBgView.image=[Tool resizeImageWithImage:navBgImg left:0 right:0 top:0 bottom:2];
        [self addSubview:_navBgView];
    
        CGFloat titleWith=230;
        
        self.titleLb=[[UILabel alloc]initWithFrame:CGRectMake((selfWidth-titleWith)/2, (selfHeight-21)/2+3, titleWith, 21)];
        _titleLb.backgroundColor=[UIColor clearColor];
        _titleLb.textColor=[UIColor whiteColor];
//        _titleLb.font=[UIFont boldSystemFontOfSize:FontSize_DefaultNavTitle];
        
        _titleLb.font = [UIFont systemFontOfSize:FontSize_DefaultNavTitle];
        _titleLb.minimumFontSize=FontSize_DefaultListTip-5;
        _titleLb.adjustsFontSizeToFitWidth=YES;
        _titleLb.textAlignment=NSTextAlignmentCenter;
        [self addSubview:_titleLb];
        
        UIImage *backImg_norm=ImageWithName(@"btn_nav_backGame_norm", @"png", NO);
        UIImage *backImg_high=ImageWithName(@"btn_nav_backGame_high", @"png", NO);
        CGSize size_navBtn=CGSizeMake(DefaultNavBtnWidth, selfHeight);
        
        
        self.leftBtn=[[ImageButtonV alloc]initWithFrame:CGRectMake(0, 8, size_navBtn.width, 28)];
        self.leftBtn.imagebutton.faceOne_norm=backImg_norm;
        self.leftBtn.imagebutton.faceOne_high=backImg_high;
        CenterTheButton(_leftBtn.imagebutton);
        [self.leftBtn.imagebutton showImageWith:0];
        //[self.leftBtn.titleButton setTitle:@"返回" forState:UIControlStateNormal];
        self.leftBtn.titleButton.titleLabel.font=[UIFont systemFontOfSize:13];
        [_leftBtn.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:_leftBtn];

        
        UIImage *midImg_norm=nil;
        UIImage *midImg_high=nil;

        
        
        self.midBtn=[[ImageButtonV alloc]initWithFrame:CGRectMake(selfWidth-size_navBtn.width - 40, 8, size_navBtn.width, 28)];
        self.midBtn.imagebutton.faceOne_norm=midImg_norm;
        self.midBtn.imagebutton.faceOne_high=midImg_high;
        
        CenterTheButton(_midBtn.imagebutton);
        [self.midBtn.imagebutton showImageWith:0];
        //[self.leftBtn.titleButton setTitle:@"返回" forState:UIControlStateNormal];
        self.midBtn.titleButton.titleLabel.font=[UIFont systemFontOfSize:13];
        [_midBtn.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:_midBtn];
        
        
        UIImage *roundImg_norm=ImageWithName(@"btn_nav_backGame_norm", @"png", NO);
        UIImage *roundimg_high=ImageWithName(@"btn_nav_backGame_high", @"png", NO);
        
        self.rightBtn=[[ImageButtonV alloc]initWithFrame:CGRectMake(selfWidth-size_navBtn.width , 0, 44, 44)];
        self.rightBtn.imagebutton.faceOne_norm=roundImg_norm;
        self.rightBtn.imagebutton.faceOne_high=roundimg_high;
        CenterTheButton(_rightBtn.imagebutton);
        [self.rightBtn.imagebutton showImageWith:0];
        self.rightBtn.titleButton.titleLabel.font=[UIFont systemFontOfSize:13];
//        self.rightBtn.titleButton.titleLabel.textAlignment = UITextAlignmentCenter;
        
        [_rightBtn.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:_rightBtn];
    }
    return self;
}
-(void)hideRightItem:(BOOL)hide{
    _rightBtn.hidden=hide;
    [self setNeedsLayout];
}
-(void)hideLeftItem:(BOOL)hide{
    _leftBtn.hidden=hide;
    [self setNeedsLayout];
}


-(void)layoutSubviews{
    [super layoutSubviews];
    UIImage *leftImg=_leftBtn.imagebutton.faceOne_norm;
    if (!leftImg) {
        leftImg=_leftBtn.imagebutton.faceOne_high;
    }
    if (!leftImg) {
        leftImg=_leftBtn.imagebutton.faceTwo_norm;
    }
    if (!leftImg) {
        leftImg=_leftBtn.imagebutton.faceTwo_high;
    }
    
    CGRectSetWidth(_leftBtn, MAX(DefaultNavBtnWidth, leftImg.size.width));
    CGRectSetX(_leftBtn, 0);
    
    UIImage *rightImg=_rightBtn.imagebutton.faceOne_norm;
    if (!rightImg) {
        rightImg=_rightBtn.imagebutton.faceOne_high;
    }
    if (!rightImg) {
        rightImg=_rightBtn.imagebutton.faceTwo_norm;
    }
    if (!rightImg) {
        rightImg=_rightBtn.imagebutton.faceTwo_high;
    }
    //DLog(@"leftImg.size:%@,rightImg.size:%@",NSStringFromCGSize(leftImg.size),NSStringFromCGSize(rightImg.size));

    CGRectSetWidth(_rightBtn, MAX(DefaultNavBtnWidth, rightImg.size.width));
    
    CGRectSetX(_rightBtn, CGRectGetWidth(self.frame)-CGRectGetWidth(_rightBtn.frame) - 6);

    
    CGFloat leftGap = DefaultNavBtnWidth-leftImg.size.width;
    CGFloat leftGap_half = leftGap/2;
    //DLog(@"real leftGap:%f",leftGap);

    CGFloat rightGap = DefaultNavBtnWidth-rightImg.size.width;
    CGFloat rightGap_half = rightGap/2;
    //DLog(@"real rightGap:%f",rightGap);
/*
    if (leftGap_half<=DefaultNavBtnGap) {
        leftGap=DefaultNavBtnGap;
    }
    else{*/
        leftGap=(DefaultNavBtnGap-leftGap_half)*2;
   // }
   // DLog(@"result leftGap:%f",leftGap);
/*
    if (rightGap_half<=DefaultNavBtnGap) {
        rightGap=DefaultNavBtnGap;
    }
    else{*/
        rightGap=(rightGap_half-DefaultNavBtnGap)*2;
    //}
    //DLog(@"result rightGap:%f",rightGap);

    
    [_leftBtn.imagebutton setImageEdgeInsets:UIEdgeInsetsMake(0,leftGap, 0, 0)];
    [_leftBtn.titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0,leftGap+_leftTitleGap, 0, 0)];

    
    
    [_rightBtn.imagebutton setImageEdgeInsets:UIEdgeInsetsMake(0, rightGap + 10, 0, 0)];
    
    [_rightBtn.titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, rightGap+_rightTitleGap, 0, 0)];

    CGRectSetWidth(_titleLb,( GetXWithView(_rightBtn)+rightGap) - GetEndXWithView(_leftBtn)-leftGap);
    CGRectSetCenterX(_titleLb, kScreenWidth/2);
    FitLableHeightWithLabel(_titleLb);
    CGRectSetCenterY(_titleLb, DefaultNavHeight/2);
    
    
    
    
}

@end
